<style type="text/css">
    table {
        max-width: 750px;
    }

    .content-textbox {
        border: 1px dashed black;
        max-height: 20em;
        overflow: auto;
    }

    .content-title {
        border: 1px dashed black;
    }
</style>
<div id="kegiatan-dialog" title="Audit Kegiatan" style="display: none;">
    <input type="hidden" class="id" />
    <h1 class="judul title content-title">Judul Kegiatan</h1>
    <p>
        <span class="keterangan"></span>
    </p>
    <p class="content-textbox">
        <br />
        <img class="pic" src="#" style="float: left; margin: 1em; height: 100px; width: 100px;"/>
        <span class="sum">
        </span>
    </p>
</div>
<div id="berita-dialog" title="Audit Berita Kegiatan" style="display: none;">
    <input type="hidden" class="id" />
    <h1 class="judul title content-title">Judul Kegiatan</h1>
    <p>
        <span class="keterangan"></span>
    </p>
    <p class="content-textbox">
        <br />
        <img class="pic" src="#" style="float: left; margin: 1em; height: 100px; width: 100px;"/>
        <span class="sum">
        </span>
    </p>
</div>
<div style="min-height: 400px;">
    <table id="listkegiatan" style="margin-top: 1em;">
        <thead>
            <tr>
                <th>Judul Kegiatan</th>
                <th>Lokasi</th>
                <th>Tanggal Kegiatan</th>
                <th>Penulis</th>
                <th>Status</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if (count($kegiatanList) > 0) {
                foreach ($kegiatanList as $kegiatan) {
                    echo "<tr>";
                    echo "<td>" . $kegiatan->content_id->title . "</td>";
                    echo "<td>" . $kegiatan->location . "</td>";
                    echo "<td>" . $kegiatan->start_date . " s/d " . $kegiatan->end_date . "</td>";
                    echo "<td>" . $kegiatan->content_id->poster_id->display_name . "</td>";
                    echo "<td>" . ($kegiatan->is_approved ? "Approved" : "Pending Approval") . "</td>";
                    echo "<td>" . "<a href='javascript:void(0);' onClick='javascript:showKegDialog(" . $kegiatan->id . ");'>Audit</a>" . "</td>";
                    echo "</tr>";
                }
            } else {
                echo "<tr>";
                echo "<td style='text-align: center;' colspan='6'>" . "(tidak ada entry kegiatan menunggu approval)" . "</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>

    <h1 class="title">Audit Entry Berita Kegiatan</h1>

    <table id="listberita" style="margin-top: 1em;">
        <thead>
            <tr>
                <th>Judul Berita</th>
                <th>Kegiatan</th>
                <th>Penulis</th>
                <th>Status</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if (count($beritaList) > 0) {
                foreach ($beritaList as $berita) {
                    echo "<tr>";
                    echo "<td>" . $berita->content_id->title . "</td>";
                    echo "<td>" . "<a href='" . site_url("kegiatan/detail/" . $berita->activity_id->id) . "'>" . $berita->activity_id->content_id->title . "</a>" . "</td>";
                    echo "<td>" . $berita->content_id->poster_id->display_name . "</td>";
                    echo "<td>" . ($berita->is_approved ? "Approved" : "Pending Approval") . "</td>";
                    echo "<td>" . "<a href='javascript:void(0);' onClick='javascript:showBeritaDialog(" . $berita->id . ");'>Audit</a>" . "</td>";
                    echo "</tr>";
                }
            } else {
                echo "<tr>";
                echo "<td style='text-align: center;' colspan='6'>" . "(tidak ada entry berita kegiatan menunggu approval)" . "</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>
</div>
<script type="text/javascript">
    jQuery(document).ready(function() {
        $("#kegiatan-dialog").dialog({
            modal: true,
            autoOpen: false,
            width: "53em",
            resizeable: false,
            buttons: {
                "Batal": function() {
                    $(this).dialog("close");
                },
                "Simpan": function() {
                    if (confirm("Selesai audit dan PUBLISH draft kegiatan?")) {
                        $.ajax({
                            type: "POST",
                            url: "<?php echo site_url("kegiatan/updateApproveKegiatan"); ?>",
                            async: false,
                            data: {
                                kegiatan_id: $("#kegiatan-dialog .id").val(),
                                title: $("#kegiatan-dialog .judul").text(),
                                text: $("#kegiatan-dialog .content-textbox .sum").html()
                            },
                            beforeSend: function(xhr) {
                            },
                            success: function(result, status, xhr) {
                                window.location = "<?php echo site_url("usercp/auditkegiatan"); ?>";
                            },
                            error: function(xhr, status, error) {
                                window.location = "<?php echo site_url("usercp/auditkegiatan"); ?>";
                            }
                        });
                    }
                },
                "Tolak": function() {
                    if (confirm("Tolak dan HAPUS draft kegiatan?")) {
                        $.ajax({
                            type: "POST",
                            url: "<?php echo site_url("kegiatan/deleteKegiatan"); ?>",
                            async: false,
                            data: {
                                kegiatan_id: $("#kegiatan-dialog .id").val()
                            },
                            beforeSend: function(xhr) {
                            },
                            success: function(result, status, xhr) {
                                window.location = "<?php echo site_url("usercp/auditkegiatan"); ?>";
                            },
                            error: function(xhr, status, error) {
                                window.location = "<?php echo site_url("usercp/auditkegiatan"); ?>";
                            }
                        });
                    }
                }
            }
        });
        $("#berita-dialog").dialog({
            modal: true,
            autoOpen: false,
            width: "53em",
            resizeable: false,
            buttons: {
                "Batal": function() {
                    $(this).dialog("close");
                },
                "Simpan": function() {
                    if (confirm("Selesai audit dan PUBLISH draft berita kegiatan?")) {
                        $.ajax({
                            type: "POST",
                            url: "<?php echo site_url("kegiatan/updateApproveBeritaKegiatan"); ?>",
                            async: false,
                            data: {
                                berita_id: $("#berita-dialog .id").val(),
                                title: $("#berita-dialog .judul").text(),
                                text: $("#berita-dialog .content-textbox .sum").html()
                            },
                            beforeSend: function(xhr) {
                            },
                            success: function(result, status, xhr) {
                                window.location = "<?php echo site_url("usercp/auditkegiatan"); ?>";
                            },
                            error: function(xhr, status, error) {
                                window.location = "<?php echo site_url("usercp/auditkegiatan"); ?>";
                            }
                        });
                    }
                },
                "Tolak": function() {
                    if (confirm("Tolak dan HAPUS draft berita kegiatan?")) {
                        $.ajax({
                            type: "POST",
                            url: "<?php echo site_url("kegiatan/deleteBeritaKegiatan"); ?>",
                            async: false,
                            data: {
                                berita_id: $("#berita-dialog .id").val()
                            },
                            beforeSend: function(xhr) {
                            },
                            success: function(result, status, xhr) {
                                window.location = "<?php echo site_url("usercp/auditkegiatan"); ?>";
                            },
                            error: function(xhr, status, error) {
                                window.location = "<?php echo site_url("usercp/auditkegiatan"); ?>";
                            }
                        });
                    }
                }
            }
        });

        createTitleEditor("#kegiatan-dialog .judul", function() {
        });
        createContentEditor("#kegiatan-dialog .content-textbox .sum", function() {
        });

        createTitleEditor("#berita-dialog .judul", function() {
        });
        createContentEditor("#berita-dialog .content-textbox .sum", function() {
        });
    });

    function showKegDialog(kegId) {
        $.ajax({
            url: "<?php echo site_url("service/getDataJSON/kegbyid"); ?>",
            method: "POST",
            dataType: "json",
            data: {
                id: kegId
            },
            success: function(kegiatan) {
                $("#kegiatan-dialog .judul").text(kegiatan.content_id.title);
                $("#kegiatan-dialog .pic").prop("src", kegiatan.content_id.thumbnail_img.src);
                $("#kegiatan-dialog .content-textbox .sum").html(kegiatan.content_id.text);
                $("#kegiatan-dialog .keterangan").html("Tanggal : " + kegiatan.start_date + " s/d " + kegiatan.end_date + "" + ", bertempat di " + kegiatan.location);
                $("#kegiatan-dialog .id").val(kegiatan.id);
            }
        });

        $("#kegiatan-dialog").dialog("open");
    }

    function showBeritaDialog(brtId) {
        $.ajax({
            url: "<?php echo site_url("service/getDataJSON/bkbyid"); ?>",
            method: "POST",
            dataType: "json",
            data: {
                id: brtId
            },
            success: function(bk) {
                $("#berita-dialog .judul").text(bk.content_id.title);
                $("#berita-dialog .pic").prop("src", bk.content_id.thumbnail_img.src);
                $("#berita-dialog .content-textbox .sum").html(bk.content_id.text);
                $("#berita-dialog .keterangan").html("Kegiatan : " + bk.activity_id.content_id.title);
                $("#berita-dialog .id").val(bk.id);
            }
        });

        $("#berita-dialog").dialog("open");
    }
</script>